﻿/*
Dynamically populated values:
	0 - unique id field names with data types
	1 - unique id field names
	2 - source database name
	3 - schema name
	4 - table name
	5 - date field name
	6 - archive date (calculated using ArchivePeriodInDays)
	7 - destination database name
	8 - destination table name
	9 - table join criteria
*/

DECLARE @ids TABLE({0});

BEGIN TRAN Tran{4};	
	INSERT INTO @ids
	SELECT {1} 
	FROM [{2}].[{3}].[{4}] 
	WHERE {5} < '{6}';

	INSERT INTO [{7}].[{3}].[{8}] ({10})
	SELECT {11}
	FROM [{2}].[{3}].[{4}] src
		INNER JOIN @ids ids ON {9};

	DELETE src
	FROM [{2}].[{3}].[{4}] src
		INNER JOIN @ids ids ON {9};
COMMIT TRAN Tran{4};